package com.app.yc.ifapp.adapter;

/*
 * 作者：TanTun
 * 时间：2017/2/19
 * 邮箱：32965926@qq.com
 * 描述：ListView适配器
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.app.yc.ifapp.R;

import java.util.List;
import java.util.Map;

public class RoomAdapter extends SimpleAdapter implements OnClickListener {

    private static final String TAG = "MyBaseAdapter";
    private List<Map<String, Object>> mContentList;
    private LayoutInflater mInflater;
    private Callback mCallback;

    public interface Callback {
        void click(View v);
    }

    public RoomAdapter(Context context,
                       List<Map<String, Object>> data, int resource,
                       String[] from, int[] to, Callback callback) {
        super(context, data, resource, from, to);

        mContentList = data;
        mInflater = LayoutInflater.from(context);
        mCallback = callback;
    }

    @Override
    public int getCount() {
        Log.v(TAG, "getCount");
        return mContentList.size();
    }

    @Override
    public Object getItem(int position) {
        Log.v(TAG, "getItem");
        return mContentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        Log.v(TAG, "getItemId");
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.v(TAG, "getView");
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_school, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.school_name);
            holder.imageView = (ImageView) convertView.findViewById(R.id.school_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        try {
            holder.textView.setText(mContentList.get(position).get("school").toString());
            holder.textView.setTag(position);

            byte[] buffer = (byte[])mContentList.get(position).get("image");
            if (buffer != null && buffer.length > 0) {
                Bitmap img = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
                if (img != null) {
                    holder.imageView.setImageBitmap(img);
                }
            }

            holder.textView.setOnClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private class ViewHolder {
        TextView textView;
        ImageView imageView;
    }

    //响应按钮点击事件,调用子定义接口，并传入View
    @Override
    public void onClick(View v) {
        mCallback.click(v);
    }
}